package com.xique.system.service.domain.service.impl;

import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.system.bean.domain.ParkInfo;
import com.xique.system.bean.domain.SystemUser;
import com.xique.system.bean.domain.SystemUserRole;
import com.xique.system.service.domain.service.ISystemProjectDomainService;
import com.xique.system.service.service.IParkInfoService;
import com.xique.system.service.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author shd
 * @create 2022/3/12 15:22
 */
@Service
public class SystemProjectDomainServiceImpl implements ISystemProjectDomainService {

    @Autowired
    private IParkInfoService parkInfoService;

    @Autowired
    private ISystemUserService systemUserService;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean insertProject(ParkInfo parkInfo, SystemUser user) {

        parkInfo.setCreateUser(SecurityUtils.getUserId());
        parkInfo.setCreateTime(DateUtils.getNowDate());
        parkInfo.setUpdateTime(DateUtils.getNowDate());
        parkInfo.setUpdateUser(SecurityUtils.getUserId());
        boolean isSuccess = parkInfoService.save(parkInfo);
        if (!isSuccess) {
            return false;
        }

        if (StringUtils.isNotNull(user)) {
            user.setParkId(parkInfo.getId());
            int count = systemUserService.insertUser(user);
            if (count <= 0) {
                return false;
            }
        }

        return true;
    }
}
